/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package schedulingScheme;

import helper.xls.TemplateHelper;
import helper.xls.TemplateProducer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import jxl.read.biff.BiffException;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import network.NetworkInfo;

/**
 *
 * @author Andrzej
 */
public class Stats {
    private NetworkInfo networkInfo;
    private Collection<POI> pois;
    private Collection<SensorNode> sensors;
    private Map<SensorNode, Collection<POI>> sensorPoiMapping = new HashMap<SensorNode, Collection<POI>>();

    private ArrayList<Integer> timeHistory = new ArrayList<Integer>();
    private ArrayList<Double> coverageHistory = new ArrayList<Double>();
    private ArrayList<Integer> activeSensorsHistory = new ArrayList<Integer>();


    public Stats(NetworkInfo networkInfo, Collection<POI> pois, Collection<SensorNode> sensors) {
        this.networkInfo = networkInfo;
        this.pois = pois;
        this.sensors = sensors;
        for (SensorNode s : sensors){
            sensorPoiMapping.put(s, new ArrayList<POI>());
            for (POI p : pois){
                if (s.inRange(p)){
                    sensorPoiMapping.get(s).add(p);
                }
            }
        }

    }

    /**
     * this function saves coverage and number of active sensors in history
     * @param time
     */
    public void update(int time) {

        double coverage = ((double) getMonitoredPOIs().size()) / pois.size();
        int activeSensors = getActiveSensors().size();
        if (timeHistory.size() == 0 || activeSensors != activeSensorsHistory.get(activeSensorsHistory.size() - 1) ||
                coverage != coverageHistory.get(coverageHistory.size() - 1)) {
            timeHistory.add(time);
            coverageHistory.add(coverage);
            activeSensorsHistory.add(activeSensors);
        } else {
            System.out.println("nie zapisuje");
        }
    }

    public void printStats(){
        System.out.println("Time");
        for (int time: timeHistory){
            System.out.println(time);
        }
        System.out.println("Coverage");
        for (double cov: coverageHistory){
            System.out.println(cov);
        }
        System.out.println("Active sensors");
        for (int count:activeSensorsHistory){
            System.out.println(count);
        }
    }

    /**
     * saves stats in template spreadsheet
     * @param fileName
     * @throws FileNotFoundException
     * @throws IOException
     * @throws BiffException
     * @throws WriteException
     */
    public void saveStats(String fileName) throws FileNotFoundException, IOException, BiffException, WriteException{


        String file = "template.xls";
        TemplateProducer instance = new TemplateProducer();
//        WritableWorkbook expResult = null;
        WritableWorkbook result = instance.getTemplate(fileName, file);
        WritableSheet sheet = result.getSheet(0);
        TemplateHelper.setNumber(sheet, 0, 1, timeHistory.get(0));
        TemplateHelper.setNumber(sheet, 1, 1, coverageHistory.get(0));
        TemplateHelper.setNumber(sheet, 2, 1, activeSensorsHistory.get(0));
        for (int i=1; i < timeHistory.size();  i++){
            TemplateHelper.setNumber(sheet, 0, i*2, timeHistory.get(i));
            TemplateHelper.setNumber(sheet, 0, i*2 + 1, timeHistory.get(i));
        }
        for (int i=1; i < coverageHistory.size(); i++){
            TemplateHelper.setNumber(sheet, 1, i*2, coverageHistory.get(i - 1));
            TemplateHelper.setNumber(sheet, 1, i*2 + 1, coverageHistory.get(i));
        }
        for (int i=1; i < activeSensorsHistory.size(); i++){
            TemplateHelper.setNumber(sheet, 2, i*2, activeSensorsHistory.get(i - 1));
            TemplateHelper.setNumber(sheet, 2, i*2 + 1, activeSensorsHistory.get(i));
        }
        result.write();
        result.close();
    }


    public Set<POI> getMonitoredPOIs(){
        Set<POI> res = new HashSet<POI>();
        Collection<SensorNode> activeSensors = networkInfo.activeSensors();
        for (SensorNode s: activeSensors){
            res.addAll(sensorPoiMapping.get(s));
        }
        return res;
    }
    public Collection<SensorNode> getActiveSensors(){
        return networkInfo.activeSensors();
    }



    

}
